#include "userinfowindow.h"
#include "inputwindow.h"
#include "resultwindow.h"
#include "advicewindow.h"
#include "goalwindow.h"
#include <QApplication>
#include <QMessageBox>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    UserInfoWindow userInfoWindow;
    InputWindow *inputWindow = nullptr;
    ResultWindow *resultWindow = nullptr;
    AdviceWindow *adviceWindow = nullptr;
    GoalWindow *goalWindow = nullptr;

    QObject::connect(&userInfoWindow, &UserInfoWindow::userAuthenticated, [&](const QString &username) {
        QMessageBox::information(nullptr, "欢迎", QString("欢迎%1使用BMI体质评估与测试系统").arg(username));
        
        if (inputWindow) inputWindow->deleteLater();
        inputWindow = new InputWindow(username);
        inputWindow->show();
        userInfoWindow.hide();

        QObject::connect(inputWindow, &InputWindow::dataSubmitted, [&](double height, double weight, int age, const QString &gender) {
            double bmi = weight / ((height/100) * (height/100));
            
            if (resultWindow) resultWindow->deleteLater();
            resultWindow = new ResultWindow(bmi, username);
            resultWindow->show();
            inputWindow->hide();

            QObject::connect(resultWindow, &ResultWindow::requestAdvice, [&]() {
                QString advice = "根据您的BMI和基本信息，建议：\n";
                if (bmi < 18.5) advice += "- 增加营养摄入\n- 进行力量训练";
                else if (bmi < 24) advice += "- 保持当前生活习惯\n- 定期体检";
                else advice += "- 控制饮食\n- 增加运动量";
                
                if (adviceWindow) adviceWindow->deleteLater();
                adviceWindow = new AdviceWindow();
                adviceWindow->setAdviceText(advice);
                QObject::connect(adviceWindow, &AdviceWindow::returnToPrevious, [&]() {
                    resultWindow->show();
                    adviceWindow->hide();
                });
                adviceWindow->show();
                resultWindow->hide();
            });

            QObject::connect(resultWindow, &ResultWindow::requestGoalSetting, [&]() {
                if (goalWindow) goalWindow->deleteLater();
                goalWindow = new GoalWindow(resultWindow);
                QObject::connect(goalWindow, &GoalWindow::returnToPrevious, [&]() {
                    resultWindow->show();
                    goalWindow->hide();
                });
                goalWindow->show();
                resultWindow->hide();
            });

            QObject::connect(resultWindow, &ResultWindow::requestHistory, [&]() {
                // 显示历史记录功能
                inputWindow->showHistory();
                resultWindow->hide();
            });

            QObject::connect(resultWindow, &ResultWindow::requestTrendPrediction, [&]() {
                // 显示BMI趋势预测功能
                resultWindow->showTrendPrediction();
            });
        });
    });

    userInfoWindow.show();
    return a.exec();
}